
  const cloud = require('wx-server-sdk')
  cloud.init({
    env: "test4-ht5oe"
  })

  const db = cloud.database()
  const _ = db.command
  const MAX_LIMIT = 100
  exports.main = async (event, context) => {
    // 先取出集合记录总数
    const countResult = await db.collection('staffAchievement').count()
    const total = countResult.total
    // 计算需分几次取
    const batchTimes = Math.ceil(total / 100)
    // 承载所有读操作的 promise 的数组
    const tasks = []
    for (let i = 0; i < batchTimes; i++) {
      const promise = db.collection('staffAchievement').where({
        teachID:event.Tid,
        date: _.gte(event.sDate).and(_.lte(event.eDate))
      }).orderBy('date', 'desc').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
      // console.log("哈哈哈"+JSON.stringify(promise))
      tasks.push(promise)
    }
    // 等待所有
    return (await Promise.all(tasks)).reduce((acc, cur) => {
      // console.log(acc,cur)
      return {
        data: acc.data.concat(cur.data),
        errMsg: acc.errMsg,
      }
    })
  }